import { _decorator } from 'cc';
import { PanelBase } from '../../Basic/UI/PanelBase';
import { LayerType, SoundCode } from '../../Basic/Config/BaseEnum';
import { CellMachine } from './CellMachine';
import { GameModel } from './GameModel';
import { MouseCtrl } from './MouseCtrl';
import { PropMachine } from './PropMachine';
import { OrderMachine } from './OrderMachine';
import { EventManager } from '../../Basic/Manager/EventManager';
import { EventType } from '../Config/GameEnum';
import { CallBackHandler } from '../../Basic/Model/PoolObj/CallBackHandler';
import { AudioManager } from '../../Basic/Manager/AudioManager';
import { SettingModel } from '../Setting/SettingModel';
import { GEEvent, GravityModel } from '../../Basic/Model/SDK/GravityModel';
import { GameData } from '../../Basic/Manager/DataManager';
import { GameParams } from '../../Basic/Config/DataConfig';
const { ccclass, property } = _decorator;

@ccclass('GamePanel')
export class GamePanel extends PanelBase {
  panelLayer: LayerType.MainLayer = LayerType.MainLayer;
  // @property(Node)
  // baseNode: Node;
  @property(OrderMachine)
  orderMac: OrderMachine;
  @property(CellMachine)
  cellMac: CellMachine;
  @property(PropMachine)
  propMac: PropMachine;
  @property(MouseCtrl)
  mouseCtrl: MouseCtrl;

  mouseTimer = this.showMouseCtrl.bind(this);

  override blackMask: number = 0;
  override clickBackClose: boolean = false;

  protected onLoad(): void {
    // super.onLoad();
    GameModel.Ins().basePanel = this;
    GameModel.Ins().cellMachine = this.cellMac;
    GameModel.Ins().orderMac = this.orderMac;
    GameModel.Ins().propMac = this.propMac;
  }

  protected onEnable(): void {
    super.onEnable();
    AudioManager.Singleton.loadAndPlayMusic(SoundCode.BGM_GAME);

    EventManager.Ins().register(EventType.SHOW_MOUSE_CTRL, this.showMouseCtrl, this);
    GameModel.Ins().initLevelData();
    this.orderMac.initVisitors();
    this.cellMac.initMachine(GameModel.Ins().curLevelData.size[0], GameModel.Ins().curLevelData.size[1]);
    this.propMac.initPropList(GameData.get(GameParams.gameLevel) < 3 ? [] : ['item_box', 'item_scoop1', 'item_scoop2']);
    this.propMac.initGridCount(GameModel.Ins().curLevelData.step);

    const tsc = CallBackHandler.pop();
    const tmc = CallBackHandler.pop();
    const tec = CallBackHandler.pop();
    tsc.callBack = this.cellMac.onTouchStart;
    tmc.callBack = this.cellMac.onTouchMove;
    tec.callBack = this.cellMac.onTouchEnd;
    tsc.target = this.cellMac;
    tmc.target = this.cellMac;
    tec.target = this.cellMac;
    this.mouseCtrl.registEvent(tsc, tmc, tec);

    const key = GameData.get(GameParams.gameLevel);
    if (GameData.get(GameParams.gameChallenge)[key] != null && GameData.get(GameParams.gameChallenge)[key] != undefined) {
      GameData.get(GameParams.gameChallenge)[key]++;
    } else {
      GameData.get(GameParams.gameChallenge)[key] = 1;
    }
    // GravityModel.I.track(GEEvent.ENTER_STAGE, { stageID: `第${key}关` });
  }

  protected onDisable(): void {
    EventManager.Ins().unRegister(EventType.SHOW_MOUSE_CTRL, this.showMouseCtrl);
    this.showMouseCtrl(true);
    this.orderMac.resetVisitors();
    this.cellMac.resetMachine();
    this.propMac.resetPropList();
    this.mouseCtrl.resetEvent();
  }

  showMouseCtrl(isShow: boolean = true) {
    this.mouseCtrl.node.active = isShow;
  }

  /** 点击检测 */
  onClickCheck() {
    // this.cellMac.checkMachine();
  }

  onClickBtn(t: Touch, data) {
    if (!GameModel.Ins().cellMachine.releaseEnable) return;
    const key = data.toString();
    AudioManager.Singleton.playSound(SoundCode.BTN_CLICK);
    switch (key) {
      case 'setting':
        SettingModel.Ins().Show();
        break;
    }
  }
}
